package com.sdjzu.wangfuying.disableautobrightness.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import java.lang.reflect.Method;

public class PackageUtil {
    private static final String TAG = "PackageUtil";

    public static String getApplicationNameByPackageName(Context context, String packageName) {
        PackageManager pm = context.getPackageManager();
        String name;
        try {
            name = pm.getApplicationLabel(pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA)).toString();
        } catch (PackageManager.NameNotFoundException e) {
            name = "";
        }
        return name;
    }

    @SuppressLint("ObsoleteSdkInt")
    public void getPackageInfo(Context context, String packageName, IPackageStatsObserver.Stub packageStatsObserver) {
        PackageManager pm = context.getPackageManager();
        try {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                Method getPackageSizeInfo = pm.getClass().getMethod(
                        "getPackageSizeInfo", String.class,
                        IPackageStatsObserver.class);
                getPackageSizeInfo.invoke(pm, packageName, packageStatsObserver);
            } else {
                Log.d(TAG, "api23及其以上版本请使用StorageManager获取App大小");
            }
        } catch (Exception e) {
        }
    }
}
